package com.yupi.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yupi.oj.model.dto.questioninfo.JudgeConfig;
import com.yupi.oj.model.entity.QuestionInfo;
import com.yupi.oj.model.entity.User;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 问题信息视图
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@Data
public class QuestionInfoVO implements Serializable {

    private final static Gson GSON = new Gson();

    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题配置(json对象)
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户的信息
     */
    private UserVO user;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;
    

    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *
     * @param questionInfoVO
     * @return
     */
    public static QuestionInfo voToObj(QuestionInfoVO questionInfoVO) {
        if (questionInfoVO == null) {
            return null;
        }
        QuestionInfo questionInfo = new QuestionInfo();
        BeanUtils.copyProperties(questionInfoVO, questionInfo);
        List<String> tagList = questionInfoVO.getTags();
        if (tagList != null) {
            questionInfo.setTags(GSON.toJson(tagList));//将json对象转化为json字符串
        }
        JudgeConfig voJudgeConfig = questionInfoVO.getJudgeConfig();
        if(voJudgeConfig != null){
            questionInfo.setJudgeConfig(JSONUtil.toJsonStr(voJudgeConfig));
        }
        return questionInfo;
    }

    /**
     * 对象转包装类
     *
     * @param questionInfo
     * @return
     */
    public static QuestionInfoVO objToVo(QuestionInfo questionInfo) {
        if (questionInfo == null) {
            return null;
        }
        QuestionInfoVO questionInfoVO = new QuestionInfoVO();
        BeanUtils.copyProperties(questionInfo, questionInfoVO);
        //json字符串转化为json对象
        List<String> tagList = JSONUtil.toList(questionInfo.getTags(), String.class);
        questionInfoVO.setTags(tagList);
        JudgeConfig judgeConfig1 = JSONUtil.toBean(questionInfo.getJudgeConfig(), JudgeConfig.class);
        questionInfoVO.setJudgeConfig(judgeConfig1);
        return questionInfoVO;
    }
}
